#!/usr/bin/env python
# -*- coding: utf-8 -*-

from typing import Optional


mock_projects = [
    {
        'name': 'mock uitest project',
        'owner': 'admin',
        'type': 'UiTest',
        'board': 'rk3568',
        'product': 'Phone',
        'product': 'hos',
        'version': '5.0',
        'create': '2023-01-01',
        'edit': '2023-12-31',
        'publish': '2023-12-31',
        'description': 'mock uitest project',
    },
    {
        'name': 'mock unittest project',
        'owner': 'mock user1',
        'type': 'UnitTest',
        'board': 'rk3588',
        'product': 'PC',
        'product': 'khos',
        'version': '4.1',
        'create': '2023-01-01',
        'edit': '2023-12-31',
        'publish': '2023-12-31',
        'description': 'mock unittest project',
    },
    {
        'name': 'mock apitest project',
        'owner': 'mock user2',
        'type': 'ApiTest',
        'board': 'rk3568',
        'product': 'Phone',
        'product': 'hos',
        'version': '5.0',
        'create': '2023-01-01',
        'edit': '2023-12-31',
        'publish': '2023-12-31',
        'description': 'mock apitest project',
    },
]

mock_test_cases = {
    'click_test':  {
        'auhor': 'mock user1',
        'create': '2023-01-01',
        'edit': '2023-12-31',
        'publish': '2023-12-31',
        'description': 'click test case',
        'workscopes': [
            {
                'id': 0,
                'title': 'Use Desktop',
                'typename': 'desktop',
                'target': 'http://127.0.0.1/desktop.jpeg',
                'properties': {
                    'anchor': None,
                    'device': {
                        'name': 'device1',
                        'board': 'rk3568',
                        'product': 'Phone',
                        'platform': 'hos',
                        'version': '5.0',
                    },
                    'timings': {
                        'delayBefore': 1000,
                        'delayAfter': 1000,
                        'timeout': 10000,
                    },
                    'options': {},
                    'input': 'input_var',
                    'output': 'output_var',
                    'continueOnError': False,
                },
                'activities': [
                    {
                        'id': 1,
                        'title': 'click_settings_icon',
                        'typename': 'click',
                        'target': 'http://127.0.0.1/settings_icon.jpeg',
                        'properties': {
                            'anchor': None,
                            'device': {
                                'name': 'device1',
                                'board': 'rk3568',
                                'product': 'Phone',
                                'platform': 'hos',
                                'version': '5.0',
                            },
                            'timings': {
                                'delayBefore': 1000,
                                'delayAfter': 1000,
                                'timeout': 10000,
                            },
                            'options': {},
                            'input': 'input_var',
                            'output': 'output_var',
                            'continueOnError': False,
                        },
                    },
                    {
                        'id': 2,
                        'title': 'click_settings_wlan_icon',
                        'typename': 'click',
                        'target': 'http://127.0.0.1/settings_wlan_icon.jpeg',
                        'properties': {
                            'anchor': None,
                            'device': {
                                'name': 'device1',
                                'board': 'rk3568',
                                'product': 'Phone',
                                'platform': 'hos',
                                'version': '5.0',
                            },
                            'timings': {
                                'delayBefore': 1000,
                                'delayAfter': 1000,
                                'timeout': 10000,
                            },
                            'options': {},
                            'input': 'input_var',
                            'output': 'output_var',
                            'continueOnError': False,
                        },
                    }
                ]
            },
        ],
    },
}

mock_test_tasks = { 
    'click_task': {
        'auhor':'mock user1',
        'create': '2023-01-01',
        'edit': '2023-12-31',
        'publish': '2023-12-31',
        'description': 'click test task',
        'tests': [ 'click_test', 'text_test' ]
    },
    'text_task': {
        'auhor':'mock user1',
        'create': '2023-01-01',
        'edit': '2023-12-31',
        'publish': '2023-12-31',
        'description': 'text test task',
        'tests': [ 'text_test' ]
    }
}

class ProjectMgr:

    def __init__(self, app, session):
        self.app = app
        self.session = session
        self.apis = {
            'get_projects': self.get_projcets,
            'rename_project': self.rename_project,
            'open_project': self.open_project,
            'set_project': self.set_project,
            'run_project': self.run_project,
            'load_test_cases': self.load_test_cases,
            'open_test_case': self.open_test_case,
            'get_test_tasks': self.get_test_tasks,
            # 'open_test_task': self.open_test_task,
        }
        
    def destroy(self):
        """销毁项目"""
        if self.session:
            self.session = None
        if self.app:
            self.app = None

    def __del__(self):
        self.destroy()

    def get_projcets(
            self,
            search: Optional[str] = None,
            owner: Optional[str] = None,
            type: Optional[str] = None,
            board: Optional[str] = None,
            product: Optional[str] = None,
            platform: Optional[str] = None,
            version: Optional[str] = None,
            start_date: Optional[str] = None,
            end_date: Optional[str] = None,
        ):
        """获取项目列表"""
        projects = mock_projects
        if search or owner or type or board or product or platform or version or start_date or end_date:
            projects = mock_projects[0:1]
        return { 'code': 200, 'content': projects }

    def rename_project(self, old_name: str, owner: str, new_name: str):
        """重命名项目"""
        return { 'code': 200, 'content': { 'message': 'rename project success' } }

    def open_project(self, owner: str, name: str):
        """打开项目"""
        if owner == 'admin':
            return { 'code': 500, 'content': {'message':'permission denied' } }
        return { 'code': 200, 'content': mock_projects[0]  }

    def set_project(self, owner: str, name: str, project_data: dict):
        """设置项目"""
        return { 'code': 200, 'content': {'message':'set project success' } }
    
    def run_project(self, owner: str, name: str):
        """运行项目"""
        return { 'code': 200, 'content': {'message':'run project success' } }

    def load_test_cases(self, owner: str, name: str):
        """获取测试用例"""
        cases = [ case for case in mock_test_cases.keys() ]
        return { 'code': 200, 'content': cases }

    def open_test_case(self, owner: str, name: str, test: str):
        """获取测试活动"""
        return { 'code': 200, 'content': mock_test_cases[test]['workscopes'] }

    def get_test_tasks(self, owner: str, name: str):
        """获取测试任务"""
        tasks = [ task for task in mock_test_tasks.keys() ]
        return { 'code': 200, 'content': tasks }